﻿using System;
using System.Collections.Generic;
using System.Drawing;
using FieldDevice.ModBus;
using FieldLib;
namespace FieldDevice.Devices
{
    class DeviceLB10:Device
    {
        public DeviceLB10(byte address, string name)
            : base(DeviceCategory.Temperature, address, name)
        {
            Initialize();
        }
        
        public DeviceLB10(byte address)
            : base( DeviceCategory.Temperature, address)
        {
            Initialize();
        }

        void Initialize()
        {
           base.RequestRegister(Temperature_A, 7);
           this.RequestRegister(Alarm_Output, 1);

           base.Thresholds.Add("Temper_A", new FloatValueThreshold("A相温度", true, 60.0f, "温度过高", false, 0, null, "℃"));
           base.Thresholds.Add("Temper_B", new FloatValueThreshold("B相温度", true, 60.0f, "温度过高", false, 0, null, "℃"));
           base.Thresholds.Add("Temper_C", new FloatValueThreshold("C相温度", true, 60.0f, "温度过高", false, 0, null, "℃"));
        }

        byte[] RegistersValue = new byte[522];

        public const ushort Temperature_A = 0;      //A相温度及寄存器地址
        public const ushort Temperature_B = 1;      //B相温度的寄存器地址
        public const ushort Temperature_C = 2;      //C相温度的寄存器地址

        public const ushort Alarm_Output = 0x0000;      //继电器输出寄存器地址
                                                        // bit0为风机控制 bit1为超温跳闸 bit2为超温报警
                                                        //bit3为铁心超温报警 bit4为故障报警

        protected override void RequestRegister(ushort registeraddress, ushort datanumber)
        {
            ModBusPacket modframe = new ModBusPacket();
            modframe.Function = ModBus.ModBusFunction.READINPUTREGISTER;
            modframe.SlaveAddress = (byte)this.ModbusAddress;

            modframe.DataAddressHi = (byte)(registeraddress >> 8);
            modframe.DataAddressLow = (byte)(registeraddress & 0x00FF);
            modframe.DataNumberHi = 0;
            modframe.DataNumberLow = (byte)(datanumber & 0x00FF);
            this.requests.Add(modframe);
        }

        public override void DebugReads()
        {
            if (DeviceManager.GlobalDeviceManager.ModBusIO.IsEmulation)
            {
                RegistersValue = base.ReadRegister(Temperature_A, 5);
                FieldLib.ModBusPacket packet = new FieldLib.ModBusPacket(this.ModbusAddress, this.ModBusReadCommand,
                   Temperature_A, 5);
                TranslateRegister(packet, RegistersValue);
                RMOutput(Alarm_Output, 1);
                base.FireReadChangeEvent("ALL", 0.0f);
            }
            else
                base.DebugReads();
        }


        protected override float Translate(byte[] value, int begin, ushort registeraddress)
        {
            int tmpValue = value[begin];
            tmpValue <<= 8;
            tmpValue += value[begin + 1];
            switch (registeraddress)
            {
                case Temperature_A:
                    RecordRead("Temper_A", tmpValue / 10.0f);
                    break;
                case Temperature_B:
                    RecordRead("Temper_B", tmpValue / 10.0f);
                    break;
                case Temperature_C:
                    RecordRead("Temper_C", tmpValue / 10.0f);
                    break;
                default:
                    break;
            }
            return 0.0f;
        }

        public override void TranslateRegister(ModBusPacket request, byte[] response)
        {
            if (response[1] == 4)
            {
                TranslateRM(response);
            }
            else
                base.TranslateRegister(request, response);
        }

        public void RMOutput(ushort registeraddress, ushort datanumber)
        {
            ModBusPacket modframe = new ModBusPacket();
            modframe.Function = FieldDevice.ModBus.ModBusFunction.READINPUTREGISTER;
            modframe.SlaveAddress = (byte)this.ModbusAddress;

            modframe.DataAddressHi = (byte)(registeraddress >> 8);
            modframe.DataAddressLow = (byte)(registeraddress & 0x00FF);
            modframe.DataNumberHi = 0;
            modframe.DataNumberLow = (byte)(datanumber & 0x00FF);
            byte[] resultbytes = this.ReadRegister(modframe);
            TranslateRM(resultbytes);
        }

        public void TranslateRM(byte[] results)         //温度显示
        {
            if (results.Length <= 3)                       //判断通信得到的数据是否小于三个
            {
                //   throw new Exception("Datas Wrong");
                return;
            }
            else
            {
                byte modbus_address = results[0];
                if ((ushort)modbus_address != this.ModbusAddress)
                    System.Diagnostics.Trace.Write("address problem");

                if (results.Length != results[2] + 5)       //判断返回的数据是否完整
                    //    throw new Exception("Data Wrong");
                    System.Diagnostics.Trace.Write("Data Wrong");
                short tmpValue = results[3];
                tmpValue <<= 8;
                tmpValue += results[4];
                if ((tmpValue & 0x0001) != 0)                        //bit0风机控制
                {
                    RecordRead("WM_Control", 1);       
                }
                else
                    RecordRead("WM_Control", 0);
                if ((tmpValue & 0x0002) != 0)                           //bit1为超温跳闸
                {
                    RecordRead("OverTemperature_Trip", 1);
                }
                else
                    RecordRead("OverTemperature_Trip", 0);

                if ((tmpValue & 0x0004) != 0)
                {
                    RecordRead("OverTemperature_Alarm", 1);             // bit2代表超温报警
                }
                else
                    RecordRead("OverTemperature_Alarm", 0);

                if ((tmpValue & 0x0008) != 0)
                {
                    RecordRead("Slug_OverTemperature", 1);             //bit3为铁心超温报警
                }
                else
                    RecordRead("Slug_OverTemperature", 0);
                if((tmpValue&0x0010)!=0)
                {
                    RecordRead("Fault_Alarm",1);                    //故障报警
                }
                else
                    RecordRead("Fault_Alarm",0);
            }

        }

        public override void Draw(System.Drawing.Graphics g, System.Drawing.Pen pen, bool isSelected, System.Drawing.SizeF zoom, System.Drawing.Point offset)
        {
            SolidBrush brush = new SolidBrush(pen.Color);
            Point p = offset;
            brush.Color = Color.Yellow;
            g.DrawString("A相温度:" + this.CurrentReads["Temper_A"].ToString("F1") + "℃", font, brush, p.X, p.Y);
            brush.Color = Color.Green;
            g.DrawString("B相温度:" + this.CurrentReads["Temper_B"].ToString("F1") + "℃", font, brush, p.X, p.Y + 24);
            brush.Color = Color.Red;
            g.DrawString("C相温度:" + this.CurrentReads["Temper_C"].ToString("F1") + "℃", font, brush, p.X, p.Y + 48);
        }

    }
}
